#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{
  pid_t id = fork();
  if(id == 0)
  {
    //子进程
    while(1)
    {
       printf("我是子进程,pid:%d,ppid:%d\n",getpid(),getppid());
       sleep(15);
       break;
    }
    exit(11);
  }else{
    //父进程
    //采用非阻塞等待
    int status = 0;
    while(1)
    {  
     pid_t ret = waitpid(-1,&status,WNOHANG);
     if(ret > 0) //等待成功
     {
       if(WIFEXITED(status))
       {
          printf("子进程正常退出,父进程等待成功,退出码是:%d\n",WEXITSTATUS(status));
       }else{
          printf("子进程非正常退出,退出信号:%d\n",status&0x7F);
       }
       break;
     }else if(ret == 0){
       printf("子进程没有退出,父进程做自己的事情\n");
       sleep(1);
     }else{
       printf("等待错误\n");
     }
    } 
    sleep(3);
  }
  return 0;
}
